//
//  LoginViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/26.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "LoginViewController.h"
#import "ChangePasswordCellTableViewCell.h"
#import "ForgetPasswordViewController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) UIView *headerView;
@property(nonatomic,strong) UIView *footerView;
@property(nonatomic,strong) NSArray *dataArray;
//// 忘记密码
@property(nonatomic,strong) UIButton *forgetPasswordBtn;
@property(nonatomic,strong) UIButton *registerBtn;
@property(nonatomic,strong) UITextField *telephoneTextField;
@property(nonatomic,strong) UITextField *pwdTextField;

@end

@implementation LoginViewController(Networking)

- (void)login
{
    if ([self.telephoneTextField.text isTextNil])
    {
        [[HUDHelper sharedInstance]tipMessage:@"请输入手机号"];
        return;
    }
    if ([self.pwdTextField.text isTextNil])
    {
        [[HUDHelper sharedInstance]tipMessage:@"请输入密码"];
        return;
    }
    
    NSDictionary *dic = @{
                          @"mobile":self.telephoneTextField.text,
                          @"pass":self.pwdTextField.text,
                          @"address":[LocationTool shareIntance].address?[LocationTool shareIntance].address:@"",
                          @"lng":@([LocationTool shareIntance].longitude),
                          @"lat":@([LocationTool shareIntance].latitude),
                          @"platform":@"2"
                          };
    WS(ws)
    [[HUDHelper sharedInstance]syncLoading:@"登录中..."];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/login/login") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [[HUDHelper sharedInstance]syncStopLoading];
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:@"登录失败"];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                UserModel *model = [UserModel yy_modelWithDictionary:obj[@"data"]];
                [[UserModel intance]setIntance:model];
                [UserModel saveLogin];
                [[HUDHelper sharedInstance]tipMessage:@"登录成功"];
                [ws.navigationController popViewControllerAnimated:YES];
                
            }
            else
            {
                [[HUDHelper sharedInstance]tipMessage:@"登录失败"];
            }
        }
    }];
}

@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //键盘回收
//    [self setupForDismissKeyboard];
    
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255/255.0 green:187/255.0 blue:2/255.0 alpha:1];
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"登录";
    
    [self.view addSubview:self.tableView];
//    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//    [button setImage:[UIImage imageNamed:@"nav_back_white"] forState:UIControlStateNormal];
//
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
//    [button addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark -- btnAction

-(void)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

//登录
- (void)loginBtnAction{
    NSLog(@"登录");
    [self.view endEditing:YES];
    [self login];
}

#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    cell.backgroundColor = getColor(whiteColor);
    return cell;
}

#pragma mark -- init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) style:UITableViewStylePlain];
        _tableView.backgroundColor = getColor(bgColor);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        _headerView.backgroundColor = getColor(bgColor);
        
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _footerView.backgroundColor = getColor(bgColor);
        
        //用户头像
        int iconImgViewW = 100;
        int iconImgViewY = 40;
        int border = 10;
        int userImageW = 20;
        UIImageView *iconImgView = [[UIImageView alloc] init];
        iconImgView.image = [UIImage imageNamed:@"user_photo"];
        iconImgView.frame = CGRectMake(0, iconImgViewY, iconImgViewW, iconImgViewW);
        iconImgView.centerX = SCREEN_WIDTH / 2;
//        iconImgView.backgroundColor = [UIColor redColor];
        iconImgView.layer.cornerRadius = iconImgViewW / 2;
        iconImgView.clipsToBounds = YES;
        
        int bgViewH = 89;
        UIView * bgView = [[UIView alloc] init];
        bgView.frame = CGRectMake(0, iconImgView.bottom + 2*border, SCREEN_WIDTH, bgViewH);
        bgView.backgroundColor = [UIColor whiteColor];
        [_footerView addSubview:bgView];
        
        //用户图标
        int userImageY = ((bgView.height - 1)/2 - userImageW)/2;
        UIImageView *userImgView = [[UIImageView alloc] init];
        userImgView.image = [UIImage imageNamed:@"login_icon_accaunt"];
        userImgView.frame = CGRectMake(border, userImageY, userImageW, userImageW);
        
        //账号
        self.telephoneTextField = [[UITextField alloc] init];
        self.telephoneTextField.font = DEF_FontSize_14;
        self.telephoneTextField.textColor = getColor(blackColor);
        self.telephoneTextField.textAlignment = NSTextAlignmentLeft;
        self.telephoneTextField.placeholder = @"请输入手机号";
        self.telephoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.telephoneTextField.clearsOnBeginEditing = YES;
        self.telephoneTextField.frame = CGRectMake(userImgView.right +3, 0, SCREEN_WIDTH, (bgView.height - 1)/2);
        
        //线
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = getColor(bgColor);
        lineView.frame = CGRectMake(0, self.telephoneTextField.bottom, SCREEN_WIDTH, 1);
        
        //密码图片
        UIImageView *pwdImgView = [[UIImageView alloc] init];
        pwdImgView.image = [UIImage imageNamed:@"longin_password"];
        pwdImgView.frame = CGRectMake(userImgView.x, lineView.bottom + userImgView.y, userImgView.width, userImgView.height);
        
        //密码
        self.pwdTextField = [[UITextField alloc] init];
        self.pwdTextField.font = DEF_FontSize_14;
        self.pwdTextField.textColor = getColor(blackColor);
        self.pwdTextField.textAlignment = NSTextAlignmentLeft;
        self.pwdTextField.placeholder = @"请输入密码";
        self.pwdTextField.secureTextEntry = YES;
        self.pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.pwdTextField.clearsOnBeginEditing = YES;
        self.pwdTextField.frame = CGRectMake(self.telephoneTextField.x, lineView.bottom, self.telephoneTextField.width, self.telephoneTextField.height);
        
        //登录按钮
        UIButton *loginButton = [[UIButton alloc] init];
        loginButton.titleLabel.font = DEF_FontSize_18;
        [loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [loginButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [loginButton setBackgroundColor:getColor(@"ffbb02")];
        loginButton.layer.cornerRadius = 10;
        loginButton.layer.masksToBounds = YES;
        [loginButton addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
        loginButton.frame = CGRectMake(border, bgView.bottom + 30, SCREEN_WIDTH - 2*border, 30/WIDTH_5S_SCALE);
        
        //添加控件
        [_footerView addSubview:iconImgView];
        [bgView addSubview:userImgView];
        [bgView addSubview:self.telephoneTextField];
        [bgView addSubview:lineView];
        [bgView addSubview:pwdImgView];
        [bgView addSubview:self.pwdTextField];
        [_footerView addSubview:loginButton];
        
        [_footerView addSubview:self.forgetPasswordBtn];
        [_footerView addSubview:self.registerBtn];
        
        int bottomBtnH = 20;
        int bottomBtnW = 100;
        self.forgetPasswordBtn.frame = CGRectMake(loginButton.left, loginButton.bottom + border, bottomBtnW, bottomBtnH);
        self.registerBtn.frame = CGRectMake(SCREEN_WIDTH - bottomBtnW - self.forgetPasswordBtn.left, self.forgetPasswordBtn.top, bottomBtnW, bottomBtnH);
        
    }
    return _footerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}

- (UIButton *)forgetPasswordBtn
{
    if (!_forgetPasswordBtn) {
        _forgetPasswordBtn = [[UIButton alloc]init];
        _forgetPasswordBtn.backgroundColor = [UIColor clearColor];
        [_forgetPasswordBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetPasswordBtn setTitleColor:getColor(blackColor) forState:UIControlStateNormal];
        _forgetPasswordBtn.titleLabel.font = DEF_FontSize_14;
        _forgetPasswordBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_forgetPasswordBtn addTarget:self action:@selector(forgetPasswordBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _forgetPasswordBtn;
}
// 忘记密码
- (void)forgetPasswordBtnAction:(UIButton*)btn
{
    NSLog(@"忘记密码");
    ForgetPasswordViewController * forgetPassword = [[ForgetPasswordViewController alloc] init];
    forgetPassword.title=@"忘记密码";
    [self.navigationController pushViewController:forgetPassword animated:YES];
}

- (UIButton *)registerBtn
{
    if (!_registerBtn) {
        _registerBtn = [[UIButton alloc]init];
        _registerBtn.backgroundColor = [UIColor clearColor];
        [_registerBtn setTitle:@"注册账号" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        _registerBtn.titleLabel.font = DEF_FontSize_14;
        _registerBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [_registerBtn addTarget:self action:@selector(registerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

- (void)registerBtnAction:(UIButton *)btn{
    RegisterViewController * registerVC = [[RegisterViewController alloc] init];
    registerVC.title = @"注册";
    [self.navigationController pushViewController:registerVC animated:YES];
}

@end
